<?php
/**
 * Created by PhpStorm.
 * User: 23783
 * Date: 2018/3/13
 * Time: 17:12
 */

namespace Controllers\Category;

use Common\CommonMethod;
use Model\DataTemplateModel;

class BaseConreollers{

    protected $common;
    protected $model;
    protected $pdf;
    public function __construct()
    {
        $this->model= new DataTemplateModel();
        $this->common= new CommonMethod();
    }

    /**
     * 获取对应文档的工作流程
     * @param $document_id
     * @return array
     */
    function BaseGetWorkflowState($document_id){
        $this->model= new DataTemplateModel();
        $this->common= new CommonMethod();
        if(!empty($document_id)){
            $taskAry = array();
            $trackingAry = array();
            $data =$this->model->GetWorkflowStateData($document_id);
            if($data->result->num_rows>0){
                $dataRow = $data->Fetch();
                foreach ($dataRow as $key=>$value){
                    //公司部门反序列化
                    if($key=='UF_DEPARTMENT'){
                        $dataRow['UF_DEPARTMENT'] = $this->common->DepartmentIdByName($value);
                    }
                    //工作流参数
                    if($key=='IBLOCK_ID'){
                        $dataRow = $this->GetWorkflowParameter($value,$document_id,$dataRow);
                    }
                }
                array_push($taskAry,$dataRow);
                //审核流程步骤
//                return  $this->GetTaskList($dataRow['ID'],$dataRow['STARTED'],$dataRow['USER_NAME'],$taskAry,$trackingAry);
                return $this->GetTaskList($taskAry,$trackingAry);
            }else{
//                $pdf->CreatePdfDocument('');
                exit('找不到对应申请流程');
            }

        }else{
            echo "流程文档ID不能为空!";
        }
    }

    /**
     * 获取报销单输入参数
     * @param $iblock_id
     * @param $document_id
     * @param $dataRow
     * @return mixed
     */
    function GetWorkflowParameter($iblock_id,$document_id,$dataRow){
        $iBlockData = $this->model->GetWorkflowParameterData($iblock_id,$document_id);
        if (mysqli_num_rows($iBlockData->result) > 0) {
            while($row = mysqli_fetch_assoc($iBlockData->result)) {
                if(strtoupper($row['CODE'])=='NUMBER'){
                    $dataRow['CAPITALMONEY'] = $this->common->NumToUpper($row['VALUE']);
                    $dataRow['paras'][strtoupper($row['CODE'])] = $row['VALUE'];
                }else if(strtoupper($row['CODE'])=='TOTAL'){
                    $dataRow['CAPITALMONEY'] = $this->common->NumToUpper($row['VALUE']);
                    $dataRow['paras'][strtoupper($row['CODE'])] = $row['VALUE'];
                }else if(strtoupper($row['CODE'])=='USE'){
                    $dataRow['paras'][strtoupper($row['CODE'])] =$this->common->PropertyEnumIdByName($row['VALUE']);
                }else if(strtoupper($row['CODE'])=='TYPE'){
                    $dataRow['paras'][strtoupper($row['CODE'])] = $this->common->PropertyEnumIdByName($row['VALUE']);
                }else{
                    $dataRow['paras'][strtoupper($row['CODE'])] = $row['VALUE'];
                }
            }
        }
        return $dataRow;
    }

    /**
     * 获取流程步骤
     * @param $workflow_id
     * @param $started
     * @param $user_name
     * @param $taskAry
     * @param $trackingAry
     * @return array|string
     */
//    function GetTaskList($workflow_id,$started,$user_name,$taskAry,$trackingAry){
//        $data = $this->model->GetTaskListData($workflow_id);
//        if (mysqli_num_rows($data->result) > 0) {
//            $trackingAry['task'][0] = $started.'  '.$user_name.'提交了申请单';
//            // 输出数据
//            while($row = mysqli_fetch_assoc($data->result)) {
//                var_dump($row);
//                $result = '';
//                if($row['STATUS']==1 || ($row['STATUS']==3 && $row['IS_INLINE']=='Y')){
//                    $result =   $row['MODIFIED'].'  '.$row['name']."审批,审批结果,同意。";
//                }else if ($row['STATUS']==2){
//                    $result =   $row['MODIFIED'].'  '.$row['name'].'审批,审批结果,拒绝。';
//                }else if ($row['STATUS']==3 && $row['IS_INLINE']=='N'){
//                    $result =   $row['MODIFIED'].'  '.'审批流程结束。';
//                }else{
//                    $result =   $row['MODIFIED'].'  '.'审批流程结束。';
//                }
//                $trackingAry['task'][] = $row['modified'].$result;
//            }
//            exit();
//        } else {
//            echo "0 结果";
//        }
//        return array_merge($taskAry[0],$trackingAry);
//    }

    /**
     * 获取流程步骤【新方法】
     * @param $taskAry
     * @param $trackingAry
     * @return array
     */
    function GetTaskList($taskAry,$trackingAry){
        //新方法
        $list = $taskAry[0]['paras']['APPROVAL_PROCESS'];
        $trackingAry['task'] = $this->common->StrApprovalProcess($list);
        return array_merge($taskAry[0],$trackingAry);
    }
}